#include<iostream>
#include<string>
#include<map>
using namespace std;
typedef long long LL;
const int N = 1e4 + 10;
int arr[N];
int n;
int main()
{
	cin >> n;
	bool flag = 0;
	int sum = 0;
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		if (arr[i] == 0) {
			sum++;
		}
		if (!i) {
			continue;
		}
		if (arr[i] != arr[i - 1]) {
			flag = 1;
		}
		else {
			if (arr[i] == 1) {
				x++;
			}
			else {
				y++;
			}
		}
	}
	LL ans = 0;
	if (flag) {
		ans = n * (n - 1);
		ans -= (x + y) * 2;
		ans += sum;
	}
	else {
		ans = (1 + n) * n / 2;
		ans *= (1 - arr[0]);
	}
	cout << ans << endl;
	return 0;
}